﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Physis.Client.Models.Insurer {

    public class RiderModel : Configuration.ConfigurationItemModel {
                
        #region Private Properties

        private Core.Insurer.Rider rider = null;

        #endregion


        #region Public Properties

        public override Int64 ConfigurationItemId { set { RiderId = value; } }

        public override Core.CoreConfigurationObject ConfigurationItem { get { return rider; } }


        public Int64 RiderId {

            set {

                rider = Application.Core.Insurer.Rider (value);

                if (rider == null) {

                    rider = new Core.Insurer.Rider (Application);

                }

            }

        }

        public Core.Insurer.Rider Rider { get { return rider; } set { rider = value; } }


        public System.Web.Mvc.SelectList InsuranceTypeSelectList {

            get {

                List<Core.Insurer.InsuranceType> insuranceTypes;

                insuranceTypes = (from currentInsuranceType in Application.Core.Insurer.InsuranceTypes (false)

                                  where ((currentInsuranceType.Enabled) || (currentInsuranceType.Id == Rider.InsuranceTypeId)) // ENABLED OR IN USE ONLY 

                                  select currentInsuranceType).ToList ();

                return new System.Web.Mvc.SelectList (insuranceTypes, "Id", "Name");

            }

        }

        #endregion 


        #region Constructors

        public RiderModel (Int64 forRiderId) {

            RiderId = forRiderId;

            return;

        }

        public RiderModel (System.Collections.Specialized.NameValueCollection form) {

            UpdateValues (form);

            return;

        }

        #endregion 


        #region Public Methods

        public override void UpdateValues (System.Collections.Specialized.NameValueCollection form) {

            base.UpdateValues (form); // BASE RELOADS THE ORIGINAL OBJECT TYPE


            // ALLOW UPDATING OF READ-ONLY PROPERTIES FOR NEW PROGRAMS

            if (Rider.Id == 0) {
                
                Rider.InsuranceTypeId = Data.Readers.FormReader.Int64FromForm (form, "Rider.InsuranceTypeId");


            }

            else {

                // RESET READ-ONLY PROPERTIES FOR EXISTING PROGRAMS 

                Rider.Name = Application.Core.Insurer.Rider (Rider.Id, false).Name;

            }


            // EDITABLE PROPERTIES FOR NEW AND EXISTING 
            
            Rider.UniqueId = Data.Readers.FormReader.StringFromForm (form, "Rider.UniqueId");

            Rider.LegacyId = Data.Readers.FormReader.StringFromForm (form, "Rider.LegacyId");


            return;

        }

        #endregion 

    }

}